﻿using System.Text.RegularExpressions;
using ConsoleApp2.Impl;
using ConsoleApp2.Service;
using ConsoleApp2.Models;

namespace ConsoleApp2.Utils;

public class CheckUtils
{
    private static LocalUserSystemService localUserSystemService = new LocalUserSystemServiceImpl();
    private static List<ConsoleApp2.Models.User> users;
    private static int USER_INDEX_COUNT = 0;

    static CheckUtils()
    {
        users = localUserSystemService.GetAllUsers();
    }

    public static bool IsEmail(string email)
    {
        string pattern = @"^[^@\s]+@[^@\s]+\.[^@\s]+$";
        return Regex.IsMatch(email, pattern);
    }

    public static bool IsPhone(string userPhone)
    {
        string pattern = @"^1[3-9]\d{9}$";
        return Regex.IsMatch(userPhone, pattern);
    }

    public static string CheckPhoneRepeat(ConsoleApp2.Models.User user)
    {
        while (!CheckEmailRepeat(user))
        {
            Console.WriteLine("手机号已经重复了，请重新输入");
            user.Phone = Console.ReadLine();
        }
        return user.Phone;
    }

    public static string CheckUserRepeat(string UserName)
    {
        while (true)
        {
            foreach (var item in users)
            {
                if (item.UserName == UserName)
                {
                    Console.WriteLine("用户名已经存在，请重新输入");
                    UserName = Console.ReadLine();
                }
            }
            return UserName;
        }
    }

    public static bool CheckEmailRepeat(ConsoleApp2.Models.User user)
    {
        foreach (var item in users)
        {
            if (item.Email == user.Email)
            {
                return false;
            }
        }
        return true;
    }
}